package com.yantong.service.impl;

import com.yantong.utils.PageUtils;
import com.yantong.pojo.WorkCause;
import com.yantong.pojo.WorkCauseExample;
import com.yantong.pojo.WorkClass;
import com.yantong.mapper.WorkCauseMapper;
import com.yantong.mapper.WorkClassMapper;
import com.yantong.service.WorkCauseService;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author kanghaixin
 * @date 2018/9/3.14:17
 */

@Service
public class WorkCauseServiceImpl implements WorkCauseService {

    @Autowired
    private WorkCauseMapper workCauseMapper;
    @Autowired
    private WorkClassMapper workClassMapper;

    private static final Logger logger = LoggerFactory.getLogger(WorkCauseServiceImpl.class);

    @Override
    public PageUtils<WorkCause> listWorkCause(Long typeId, int offset, int limit) {
        WorkCauseExample workCauseExample = new WorkCauseExample();
        WorkCauseExample.Criteria criteria = workCauseExample.createCriteria();
        criteria.andIsdeleteEqualTo(0);
        if (typeId != null){
            criteria.andCidEqualTo(typeId);
        }
        PageUtils<WorkCause> pageUtils = new PageUtils<>();
        int total = workCauseMapper.countByExample(workCauseExample);
        if (total >0) {
            List<WorkCause> list = workCauseMapper.selectByExample(workCauseExample,new RowBounds(offset, limit));
            for (WorkCause workCause: list) {
                Long cid = workCause.getCid();
                if (cid == null) {
                    continue;
                }
                WorkClass workClass = workClassMapper.selectByPrimaryKey(cid);
                if (workClass == null) {
                    continue;
                }
                workCause.setcName(workClass.getTypename());
            }
            pageUtils.setRows(list);
            pageUtils.setTotal(total);
        }
        return pageUtils;
    }

    @Override
    public int save(WorkCause workCause) {
        if (workCause == null) {
            return 0;
        }
        logger.info("save work cause!");
        return workCauseMapper.insertSelective(workCause);
    }

    @Override
    public int update(WorkCause workCause) {
        if (workCause == null || workCause.getId() == null) {
            return 0;
        }
        return workCauseMapper.updateByPrimaryKeySelective(workCause);
    }

    @Override
    public WorkCause getByKey(Long causeId) {
        if (causeId == null) {
            return null;
        }
        return workCauseMapper.selectByPrimaryKey(causeId);
    }

    @Override
    public List<WorkCause> listAllCause(Long typeId) {
        WorkCauseExample workCauseExample = new WorkCauseExample();
        WorkCauseExample.Criteria criteria = workCauseExample.createCriteria();
        criteria.andIsdeleteEqualTo(0);
        if (typeId != null){
            criteria.andCidEqualTo(typeId);
        }
        return workCauseMapper.selectByExample(workCauseExample);
    }
}
